package cn.yuhh.api.validator;

import cn.yuhh.api.annotation.ValidatorClass;
import org.springframework.core.annotation.AnnotationUtils;

import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.Map;

public class Validator {

    private static Map<String, AnnotationValidator> map = new HashMap<>();



    public static Boolean validate(Class service, Map request) throws IllegalAccessException, InstantiationException {
        Annotation[] annotations = service.getAnnotations();
        for (Annotation annotation : annotations) {
            ValidatorClass validatorClass = AnnotationUtils.findAnnotation(annotation.getClass(), ValidatorClass.class);
            if (validatorClass != null) {
                Class<AnnotationValidator> clazz = validatorClass.clazz();
                AnnotationValidator validator = map.get(clazz.getName());
                if(validator == null){
                    validator =  clazz.newInstance();
                    map.put(clazz.getName(), validator);
                }
                validator.handler(annotation, request);
            }

        }
        return true;
    }


}
